import { Window_ItemList } from "@/windows/Window_ItemList.js";
import { DataManager } from "@/managers/DataManager.js";
import { config } from "@/managers/ConfigManager.js";

export class Window_EventItem extends Window_ItemList {
    constructor(messageWindow) {
        super(0, 0);

        this._messageWindow = messageWindow;
        this.deactivate();
        this.setHandler('ok',     this.onOk.bind(this));
        this.setHandler('cancel', this.onCancel.bind(this));
    }

    windowWidth(){
        return config.width;
    }
    windowHeight() {
        return this.fittingHeight(this.numVisibleRows());
    }

    numVisibleRows() {
        return 4;
    }

    start() {
        this.refresh();
        this.updatePlacement();
        this.select(0);
        this.open();
        this.activate();
    }

    updatePlacement() {
        if (this._messageWindow.y >= config.height / 2) {
            this.y = 0;
        } else {
            this.y = config.height - this.height;
        }
    }

    includes(item) {
        const itypeId = $gameMessage.itemChoiceItypeId();
        return DataManager.isItem(item) && item.itypeId === itypeId;
    }

    isEnabled(item) {
        return true;
    }

    onOk() {
        const item = this.item();
        const itemId = item ? item.id : 0;
        $gameVariables.setValue($gameMessage.itemChoiceVariableId(), itemId);
        this._messageWindow.terminateMessage();
        this.close();
    }

    onCancel() {
        $gameVariables.setValue($gameMessage.itemChoiceVariableId(), 0);
        this._messageWindow.terminateMessage();
        this.close();
    }
}